package cn.wolfcode.p2p.base.service.impl;

import cn.wolfcode.p2p.base.domain.RealAuth;
import cn.wolfcode.p2p.base.domain.UserInfo;
import cn.wolfcode.p2p.base.exception.DisplayableException;
import cn.wolfcode.p2p.base.mapper.RealAuthMapper;
import cn.wolfcode.p2p.base.query.QueryObject;
import cn.wolfcode.p2p.base.service.IRealAuthService;
import cn.wolfcode.p2p.base.service.IUserInfoService;
import cn.wolfcode.p2p.base.util.BitStateUtil;
import cn.wolfcode.p2p.base.util.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class RealAuthServiceeImpl implements IRealAuthService {

    @Autowired
    private RealAuthMapper realAuthMapper;

    @Autowired
    private IUserInfoService userInfoService;

    @Override
    public int save(RealAuth realAuth) {
        return realAuthMapper.insert(realAuth);
    }

    @Override
    public int update(RealAuth realAuth) {
        return realAuthMapper.updateByPrimaryKey(realAuth);
    }

    @Override
    public RealAuth get(Long id) {
        return realAuthMapper.selectByPrimaryKey(id);
    }

    @Override
    public PageInfo queryPage(QueryObject qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        List<RealAuth> list = realAuthMapper.selectList(qo);
        return new PageInfo(list);
    }

    /**
     * 申请实名认证逻辑
     * @param realAuth
     */
    @Override
    public void apply(RealAuth realAuth) {
        //获取当前用户的用户信息
        UserInfo userInfo = userInfoService.getCurrent();
        //UserInfo userInfo = UserContext.getUserInfoCurrent();
        //判断其RealAuthId是否为null
        if(userInfo.getRealAuthId() == null){
            //如果为null 证明当前用户是没有申请过实名认证的
            RealAuth ra = new RealAuth();
            //真实姓名
            ra.setRealName(realAuth.getRealName());
            //地址信息
            ra.setAddress(realAuth.getAddress());
            //性别
            ra.setSex(realAuth.getSex());
            //身份证号码
            ra.setIdNumber(realAuth.getIdNumber());
            //申请时间
            ra.setApplyTime(new Date());
            //申请人
            ra.setApplier(UserContext.getCurrent());
            //出生日期
            ra.setBornDate(realAuth.getBornDate());
            //image1
            ra.setImage1(realAuth.getImage1());
            //image2
            ra.setImage2(realAuth.getImage2());
            //申请状态 待审核
            ra.setState(realAuth.STATE_NORMAL);
            //保存到数据库 调用save方法
            this.save(ra);

            //需要更新realAuthid 到userInfo里面
            userInfo.setRealAuthId(ra.getId());
            userInfoService.update(userInfo);
        }else {
            throw new DisplayableException("非法操作,请重新填自己的信息!");
        }
    }

    /**
     * 后台审核功能
     * @param id 当前用户的ID
     * @param state 用户的审核状态
     * @param remark 备注信息
     */
    @Override
    public void audit(Long id, int state, String remark) {
        //根据ID 获取审核对象
        RealAuth realAuth = get(id);
        //对象存在 审核对象处于待审核状态
        if(realAuth != null && realAuth.getState() == RealAuth.STATE_NORMAL){
            //设置相关的属性 --> 审核时间 审核人
            realAuth.setAuditTime(new Date());
            realAuth.setAuditor(UserContext.getCurrent());

            //获取申请人的id
            UserInfo userInfo = userInfoService.get(realAuth.getApplier().getId());

            //如果传过来的state状态 和 审核通过一致
            if (state == RealAuth.STATE_PASS) {
                //设置审核通过状态
                realAuth.setState(RealAuth.STATE_PASS);
                //给申请人添加实名认证的状态码
                userInfo.addState(BitStateUtil.HAS_REAL_AUTH);
                //给申请人设置 realName 和 IDNumber
                userInfo.setRealName(realAuth.getRealName());
                userInfo.setIdNumber(realAuth.getIdNumber());
            }else {
                //如果审核拒绝
                realAuth.setState(RealAuth.STATE_REJECT);
                //把申请人的realAuthId 设置为null
                userInfo.setRealAuthId(null);
            }
            //更新realAuth到数据库
            update(realAuth);
            userInfoService.update(userInfo);
        }
    }
}
